package com.yiqi.liebang.feature.mine.model;

import android.text.TextUtils;

import com.suozhang.framework.component.http.RxDataProcessFactory;
import com.yiqi.liebang.entity.bo.AddTopicBo;
import com.yiqi.liebang.entity.bo.EducationBo;
import com.yiqi.liebang.feature.mine.contract.EducationContract;
import com.yiqi.liebang.feature.mine.contract.TopicContract;
import com.yiqi.liebang.feature.mine.dependencies.edu.DaggerEduModelComponent;
import com.yiqi.liebang.feature.mine.dependencies.topic.DaggerTopicModelComponent;
import com.yiqi.liebang.framework.api.ApiLib;
import com.yiqi.liebang.framework.api.UserApi;

import java.io.File;

import javax.inject.Inject;

import io.reactivex.Observable;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class EduModel implements EducationContract.Model {

    @Inject
    UserApi api;


    @Inject
    public EduModel() {
        DaggerEduModelComponent.builder().apiComponent(ApiLib.apiComponent()).build().inject(this);
    }




    @Override
    public Observable<String> userAddEducation(String eduLogo, String schoolName, String subjectName, String diploma, String beginTime, String endTime, String eduDescribe) {
        MultipartBody.Part headFilePart = null;
        if (!TextUtils.isEmpty(eduLogo)) {
            File headFile = new File(eduLogo);
            RequestBody requestFile = RequestBody.create(MediaType.parse("application/otcet-stream"), headFile);
            headFilePart = MultipartBody.Part.createFormData("eduLogo", headFile.getName(), requestFile);
        }

        RequestBody schoolNameBody = RequestBody.create(MediaType.parse("multipart/form-data"), schoolName);
        RequestBody subjectNameBody = RequestBody.create(MediaType.parse("multipart/form-data"), subjectName);
        RequestBody diplomaBody = RequestBody.create(MediaType.parse("multipart/form-data"), diploma);
        RequestBody beginTimeBody = RequestBody.create(MediaType.parse("multipart/form-data"), beginTime);
        RequestBody endTimeBody = RequestBody.create(MediaType.parse("multipart/form-data"), endTime);
        RequestBody eduDescribeBody = RequestBody.create(MediaType.parse("multipart/form-data"), eduDescribe);
        return api.addEducation(headFilePart, schoolNameBody, subjectNameBody, diplomaBody, beginTimeBody, endTimeBody, eduDescribeBody).compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer());
    }

    @Override
    public Observable<String> userEditEducation(String id, String eduLogo, String schoolName, String subjectName, String diploma, String beginTime, String endTime, String eduDescribe) {
        MultipartBody.Part headFilePart = null;
        if (!TextUtils.isEmpty(eduLogo)) {
            File headFile = new File(eduLogo);
            RequestBody requestFile = RequestBody.create(MediaType.parse("application/otcet-stream"), headFile);
            headFilePart = MultipartBody.Part.createFormData("eduLogo", headFile.getName(), requestFile);
        }
        RequestBody idBody = RequestBody.create(MediaType.parse("multipart/form-data"), id);
        RequestBody schoolNameBody = RequestBody.create(MediaType.parse("multipart/form-data"), schoolName);
        RequestBody subjectNameBody = RequestBody.create(MediaType.parse("multipart/form-data"), subjectName);
        RequestBody diplomaBody = RequestBody.create(MediaType.parse("multipart/form-data"), diploma);
        RequestBody beginTimeBody = RequestBody.create(MediaType.parse("multipart/form-data"), beginTime);
        RequestBody endTimeBody = RequestBody.create(MediaType.parse("multipart/form-data"), endTime);
        RequestBody eduDescribeBody = RequestBody.create(MediaType.parse("multipart/form-data"), eduDescribe);
        return api.editEducation(headFilePart, idBody, schoolNameBody, subjectNameBody, diplomaBody, beginTimeBody, endTimeBody, eduDescribeBody).compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer());
    }

    @Override
    public Observable<EducationBo> userGetEducation(EducationBo workBo) {
        return api.getEducation(workBo).compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer());
    }

    @Override
    public Observable<String> userDeleteEducation(EducationBo workBo) {
        return api.deleteEducation(workBo).compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer());
    }
}
